package com.dom;

import com.JUtil;
import com.Student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Studentimp  implements studentinter {
    Connection conn;
    @Override
    public void add(Student s) {
        try {
            conn= JUtil.getConn();
            //Statement st = conn.createStatement();
            String sql ="insert into student(name,age) values (?,?)";
            PreparedStatement st=conn.prepareStatement(sql);//预编译
            st.setString(1,s.getName());
            st.setInt(2,s.getAge());
            st.executeUpdate();
            JUtil.free(st,conn);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void delete(Student s) {
        try {
            conn= JUtil.getConn();
            //Statement st = conn.createStatement();
            String sql ="delete from student where id=?";
            PreparedStatement st=conn.prepareStatement(sql);//预编译
            st.setInt(1,s.getId());
            st.executeUpdate();
            JUtil.free(st,conn);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    public void updata(Student s) {
        try {
            conn= JUtil.getConn();
            //Statement st = conn.createStatement();
            String sql ="update student set age=? where id=?";
            PreparedStatement st=conn.prepareStatement(sql);//预编译
            st.setInt(1,s.getAge());
            st.setInt(2,s.getId());
            st.executeUpdate();
            JUtil.free(st,conn);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    @Override
    public Student findbyid(int id) {
        Student s=null;
        try {
            String sql="select * from student where id=?";
            conn=JUtil.getConn();
            PreparedStatement st = conn.prepareStatement(sql);
            st.setInt(1,id);
            ResultSet re = st.executeQuery();
            while(re.next()){
                s =new Student(re.getInt(1),re.getString(2),re.getInt(3));}
            re.close();
            st.close();
            conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return s;
    }

    @Override
    public List<String> findall(Student s) {
        return null;
    }
}
